<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>filewriter_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_fs_filewriter_test.js.html">filewriter_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.fs.FileWriterTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.fs.FileWriterTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.async.Deferred&#39;);
<a name="line19"></a>goog.require(&#39;goog.events&#39;);
<a name="line20"></a>goog.require(&#39;goog.fs.Error&#39;);
<a name="line21"></a>goog.require(&#39;goog.fs.FileSaver&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.AsyncTestCase&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.fs.Blob&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.fs.FileSystem&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line27"></a>
<a name="line28"></a>var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();
<a name="line29"></a>var file, deferredWriter, mockClock;
<a name="line30"></a>
<a name="line31"></a>function setUp() {
<a name="line32"></a>  mockClock = new goog.testing.MockClock(true);
<a name="line33"></a>
<a name="line34"></a>  var fs = new goog.testing.fs.FileSystem();
<a name="line35"></a>  var fileEntry = fs.getRoot().createDirectorySync(&#39;foo&#39;).createFileSync(&#39;bar&#39;);
<a name="line36"></a>
<a name="line37"></a>  deferredWriter = fileEntry.createWriter();
<a name="line38"></a>  file = fileEntry.fileSync();
<a name="line39"></a>  file.setDataInternal(&#39;&#39;);
<a name="line40"></a>}
<a name="line41"></a>
<a name="line42"></a>function tearDown() {
<a name="line43"></a>  mockClock.uninstall();
<a name="line44"></a>}
<a name="line45"></a>
<a name="line46"></a>function testWrite() {
<a name="line47"></a>  deferredWriter.
<a name="line48"></a>      addCallback(goog.partial(checkReadyState,
<a name="line49"></a>                               goog.fs.FileSaver.ReadyState.INIT)).
<a name="line50"></a>      addCallback(goog.partial(checkPositionAndLength, 0, 0)).
<a name="line51"></a>      addCallback(goog.partial(checkLastModified, 0)).
<a name="line52"></a>      addCallback(goog.partial(tick, 3)).
<a name="line53"></a>      addCallback(goog.partial(writeString, &#39;hello&#39;)).
<a name="line54"></a>      addCallback(goog.partial(checkPositionAndLength, 0, 0)).
<a name="line55"></a>      addCallback(goog.partial(checkReadyState,
<a name="line56"></a>                               goog.fs.FileSaver.ReadyState.WRITING)).
<a name="line57"></a>      addCallback(goog.partial(checkLastModified, 0)).
<a name="line58"></a>      addCallback(goog.partial(waitForEvent,
<a name="line59"></a>                               goog.fs.FileSaver.EventType.WRITE_START)).
<a name="line60"></a>      addCallback(goog.partial(checkLastModified, 0)).
<a name="line61"></a>      addCallback(goog.partial(checkPositionAndLength, 0, 0)).
<a name="line62"></a>      addCallback(goog.partial(waitForEvent,
<a name="line63"></a>                               goog.fs.FileSaver.EventType.WRITE)).
<a name="line64"></a>      addCallback(function() { assertEquals(&#39;hello&#39;, file.toString()); }).
<a name="line65"></a>      addCallback(goog.partial(checkPositionAndLength, 5, 5)).
<a name="line66"></a>      addCallback(goog.partial(checkLastModified, 3)).
<a name="line67"></a>      addCallback(tick).
<a name="line68"></a>      addCallback(goog.partial(checkReadyState,
<a name="line69"></a>                               goog.fs.FileSaver.ReadyState.WRITING)).
<a name="line70"></a>      addCallback(goog.partial(checkLastModified, 3)).
<a name="line71"></a>      addCallback(goog.partial(waitForEvent,
<a name="line72"></a>                               goog.fs.FileSaver.EventType.WRITE_END)).
<a name="line73"></a>      addCallback(goog.partial(checkLastModified, 3)).
<a name="line74"></a>      addCallback(goog.partial(checkReadyState,
<a name="line75"></a>                               goog.fs.FileSaver.ReadyState.DONE)).
<a name="line76"></a>      addCallback(goog.partial(checkLastModified, 3)).
<a name="line77"></a>      addCallback(goog.partial(writeString, &#39; world&#39;)).
<a name="line78"></a>      addCallback(goog.partial(checkPositionAndLength, 5, 5)).
<a name="line79"></a>      addCallback(goog.partial(waitForEvent,
<a name="line80"></a>                               goog.fs.FileSaver.EventType.WRITE)).
<a name="line81"></a>      addCallback(function() { assertEquals(&#39;hello world&#39;, file.toString()); }).
<a name="line82"></a>      addCallback(goog.partial(checkPositionAndLength, 11, 11)).
<a name="line83"></a>      addCallback(goog.partial(checkLastModified, 4)).
<a name="line84"></a>      addBoth(continueTesting);
<a name="line85"></a>  waitForAsync(&#39;testWrite&#39;);
<a name="line86"></a>}
<a name="line87"></a>
<a name="line88"></a>function testSeek() {
<a name="line89"></a>  deferredWriter.
<a name="line90"></a>      addCallback(goog.partial(tick, 17)).
<a name="line91"></a>      addCallback(goog.partial(writeAndWait, &#39;hello world&#39;)).
<a name="line92"></a>      addCallback(tick).
<a name="line93"></a>      addCallback(goog.partial(checkPositionAndLength, 11, 11)).
<a name="line94"></a>
<a name="line95"></a>      addCallback(function(writer) { writer.seek(6); }).
<a name="line96"></a>      addCallback(goog.partial(checkPositionAndLength, 6, 11)).
<a name="line97"></a>      addCallback(goog.partial(checkLastModified, 17)).
<a name="line98"></a>      addCallback(goog.partial(writeAndWait, &#39;universe&#39;)).
<a name="line99"></a>      addCallback(tick).
<a name="line100"></a>      addCallback(function() {
<a name="line101"></a>        assertEquals(&#39;hello universe&#39;, file.toString());
<a name="line102"></a>      }).
<a name="line103"></a>      addCallback(goog.partial(checkPositionAndLength, 14, 14)).
<a name="line104"></a>
<a name="line105"></a>      addCallback(function(writer) { writer.seek(500); }).
<a name="line106"></a>      addCallback(goog.partial(checkPositionAndLength, 14, 14)).
<a name="line107"></a>      addCallback(goog.partial(writeAndWait, &#39;!&#39;)).
<a name="line108"></a>      addCallback(tick).
<a name="line109"></a>      addCallback(function() {
<a name="line110"></a>        assertEquals(&#39;hello universe!&#39;, file.toString());
<a name="line111"></a>      }).
<a name="line112"></a>      addCallback(goog.partial(checkPositionAndLength, 15, 15)).
<a name="line113"></a>
<a name="line114"></a>      addCallback(function(writer) { writer.seek(-9); }).
<a name="line115"></a>      addCallback(goog.partial(checkPositionAndLength, 6, 15)).
<a name="line116"></a>      addCallback(goog.partial(writeAndWait, &#39;foo&#39;)).
<a name="line117"></a>      addCallback(tick).
<a name="line118"></a>      addCallback(function() {
<a name="line119"></a>        assertEquals(&#39;hello fooverse!&#39;, file.toString());
<a name="line120"></a>      }).
<a name="line121"></a>      addCallback(goog.partial(checkPositionAndLength, 9, 15)).
<a name="line122"></a>
<a name="line123"></a>      addCallback(function(writer) { writer.seek(-500); }).
<a name="line124"></a>      addCallback(goog.partial(checkPositionAndLength, 0, 15)).
<a name="line125"></a>      addCallback(goog.partial(writeAndWait, &#39;bye-o&#39;)).
<a name="line126"></a>      addCallback(tick).
<a name="line127"></a>      addCallback(function() {
<a name="line128"></a>        assertEquals(&#39;bye-o fooverse!&#39;, file.toString());
<a name="line129"></a>      }).
<a name="line130"></a>      addCallback(goog.partial(checkPositionAndLength, 5, 15)).
<a name="line131"></a>      addCallback(goog.partial(checkLastModified, 21)).
<a name="line132"></a>      addBoth(continueTesting);
<a name="line133"></a>  waitForAsync(&#39;testSeek&#39;);
<a name="line134"></a>}
<a name="line135"></a>
<a name="line136"></a>function testAbort() {
<a name="line137"></a>  deferredWriter.
<a name="line138"></a>      addCallback(goog.partial(tick, 13)).
<a name="line139"></a>      addCallback(goog.partial(writeString, &#39;hello world&#39;)).
<a name="line140"></a>      addCallback(function(writer) { writer.abort(); }).
<a name="line141"></a>      addCallback(goog.partial(checkReadyState,
<a name="line142"></a>                               goog.fs.FileSaver.ReadyState.WRITING)).
<a name="line143"></a>      addCallback(goog.partial(waitForError, goog.fs.Error.ErrorCode.ABORT)).
<a name="line144"></a>      addCallback(goog.partial(checkReadyState,
<a name="line145"></a>                               goog.fs.FileSaver.ReadyState.WRITING)).
<a name="line146"></a>      addCallback(goog.partial(waitForEvent,
<a name="line147"></a>                               goog.fs.FileSaver.EventType.ABORT)).
<a name="line148"></a>      addCallback(goog.partial(checkReadyState,
<a name="line149"></a>                               goog.fs.FileSaver.ReadyState.WRITING)).
<a name="line150"></a>      addCallback(goog.partial(waitForEvent,
<a name="line151"></a>                               goog.fs.FileSaver.EventType.WRITE_END)).
<a name="line152"></a>      addCallback(goog.partial(checkReadyState,
<a name="line153"></a>                               goog.fs.FileSaver.ReadyState.DONE)).
<a name="line154"></a>      addCallback(goog.partial(checkPositionAndLength, 0, 0)).
<a name="line155"></a>      addCallback(goog.partial(checkLastModified, 0)).
<a name="line156"></a>      addCallback(function() { assertEquals(&#39;&#39;, file.toString()); }).
<a name="line157"></a>      addBoth(continueTesting);
<a name="line158"></a>  waitForAsync(&#39;testAbort&#39;);
<a name="line159"></a>}
<a name="line160"></a>
<a name="line161"></a>function testTruncate() {
<a name="line162"></a>  deferredWriter.
<a name="line163"></a>      addCallback(goog.partial(writeAndWait, &#39;hello world&#39;)).
<a name="line164"></a>      addCallback(goog.partial(checkPositionAndLength, 11, 11)).
<a name="line165"></a>      addCallback(function(writer) { writer.truncate(5); }).
<a name="line166"></a>      addCallback(goog.partial(checkPositionAndLength, 11, 11)).
<a name="line167"></a>      addCallback(goog.partial(checkReadyState,
<a name="line168"></a>                               goog.fs.FileSaver.ReadyState.WRITING)).
<a name="line169"></a>      addCallback(goog.partial(waitForEvent,
<a name="line170"></a>                               goog.fs.FileSaver.EventType.WRITE_START)).
<a name="line171"></a>      addCallback(goog.partial(tick, 7)).
<a name="line172"></a>      addCallback(goog.partial(checkPositionAndLength, 11, 11)).
<a name="line173"></a>      addCallback(goog.partial(checkLastModified, 0)).
<a name="line174"></a>      addCallback(goog.partial(waitForEvent,
<a name="line175"></a>                               goog.fs.FileSaver.EventType.WRITE)).
<a name="line176"></a>      addCallback(goog.partial(checkLastModified, 7)).
<a name="line177"></a>      addCallback(tick).
<a name="line178"></a>      addCallback(goog.partial(checkReadyState,
<a name="line179"></a>                               goog.fs.FileSaver.ReadyState.WRITING)).
<a name="line180"></a>      addCallback(goog.partial(checkPositionAndLength, 5, 5)).
<a name="line181"></a>      addCallback(function() { assertEquals(&#39;hello&#39;, file.toString()); }).
<a name="line182"></a>      addCallback(goog.partial(waitForEvent,
<a name="line183"></a>                               goog.fs.FileSaver.EventType.WRITE_END)).
<a name="line184"></a>      addCallback(goog.partial(checkReadyState,
<a name="line185"></a>                               goog.fs.FileSaver.ReadyState.DONE)).
<a name="line186"></a>
<a name="line187"></a>      addCallback(function(writer) { writer.truncate(10); }).
<a name="line188"></a>      addCallback(goog.partial(waitForEvent,
<a name="line189"></a>                               goog.fs.FileSaver.EventType.WRITE_END)).
<a name="line190"></a>      addCallback(goog.partial(checkPositionAndLength, 5, 10)).
<a name="line191"></a>      addCallback(goog.partial(checkLastModified, 8)).
<a name="line192"></a>      addCallback(function() {
<a name="line193"></a>        assertEquals(&#39;hello\0\0\0\0\0&#39;, file.toString());
<a name="line194"></a>      }).
<a name="line195"></a>      addBoth(continueTesting);
<a name="line196"></a>  waitForAsync(&#39;testTruncate&#39;);
<a name="line197"></a>}
<a name="line198"></a>
<a name="line199"></a>function testAbortBeforeWrite() {
<a name="line200"></a>  deferredWriter.
<a name="line201"></a>      addCallback(function(writer) { writer.abort(); }).
<a name="line202"></a>      addErrback(function(err) {
<a name="line203"></a>        assertEquals(goog.fs.Error.ErrorCode.INVALID_STATE, err.code);
<a name="line204"></a>        return true;
<a name="line205"></a>      }).
<a name="line206"></a>      addCallback(function(calledErrback) {
<a name="line207"></a>        assertTrue(calledErrback);
<a name="line208"></a>      }).
<a name="line209"></a>      addBoth(continueTesting);
<a name="line210"></a>  waitForAsync(&#39;testAbortBeforeWrite&#39;);
<a name="line211"></a>}
<a name="line212"></a>
<a name="line213"></a>function testAbortAfterWrite() {
<a name="line214"></a>  deferredWriter.
<a name="line215"></a>      addCallback(goog.partial(writeAndWait, &#39;hello world&#39;)).
<a name="line216"></a>      addCallback(function(writer) { writer.abort(); }).
<a name="line217"></a>      addErrback(function(err) {
<a name="line218"></a>        assertEquals(goog.fs.Error.ErrorCode.INVALID_STATE, err.code);
<a name="line219"></a>        return true;
<a name="line220"></a>      }).
<a name="line221"></a>      addCallback(assertTrue).
<a name="line222"></a>      addBoth(continueTesting);
<a name="line223"></a>  waitForAsync(&#39;testAbortAfterWrite&#39;);
<a name="line224"></a>}
<a name="line225"></a>
<a name="line226"></a>function testWriteDuringWrite() {
<a name="line227"></a>  deferredWriter.
<a name="line228"></a>      addCallback(goog.partial(writeString, &#39;hello world&#39;)).
<a name="line229"></a>      addCallback(goog.partial(writeString, &#39;hello world&#39;)).
<a name="line230"></a>      addErrback(function(err) {
<a name="line231"></a>        assertEquals(goog.fs.Error.ErrorCode.INVALID_STATE, err.code);
<a name="line232"></a>        return true;
<a name="line233"></a>      }).
<a name="line234"></a>      addCallback(assertTrue).
<a name="line235"></a>      addBoth(continueTesting);
<a name="line236"></a>  waitForAsync(&#39;testWriteDuringWrite&#39;);
<a name="line237"></a>}
<a name="line238"></a>
<a name="line239"></a>function testSeekDuringWrite() {
<a name="line240"></a>  deferredWriter.
<a name="line241"></a>      addCallback(goog.partial(writeString, &#39;hello world&#39;)).
<a name="line242"></a>      addCallback(function(writer) { writer.seek(5); }).
<a name="line243"></a>      addErrback(function(err) {
<a name="line244"></a>        assertEquals(goog.fs.Error.ErrorCode.INVALID_STATE, err.code);
<a name="line245"></a>        return true;
<a name="line246"></a>      }).
<a name="line247"></a>      addCallback(assertTrue).
<a name="line248"></a>      addBoth(continueTesting);
<a name="line249"></a>  waitForAsync(&#39;testSeekDuringWrite&#39;);
<a name="line250"></a>}
<a name="line251"></a>
<a name="line252"></a>function testTruncateDuringWrite() {
<a name="line253"></a>  deferredWriter.
<a name="line254"></a>      addCallback(goog.partial(writeString, &#39;hello world&#39;)).
<a name="line255"></a>      addCallback(function(writer) { writer.truncate(5); }).
<a name="line256"></a>      addErrback(function(err) {
<a name="line257"></a>        assertEquals(goog.fs.Error.ErrorCode.INVALID_STATE, err.code);
<a name="line258"></a>        return true;
<a name="line259"></a>      }).
<a name="line260"></a>      addCallback(assertTrue).
<a name="line261"></a>      addBoth(continueTesting);
<a name="line262"></a>  waitForAsync(&#39;testTruncateDuringWrite&#39;);
<a name="line263"></a>}
<a name="line264"></a>
<a name="line265"></a>
<a name="line266"></a>function tick(opt_tickCount) {
<a name="line267"></a>  mockClock.tick(opt_tickCount);
<a name="line268"></a>}
<a name="line269"></a>
<a name="line270"></a>function continueTesting(result) {
<a name="line271"></a>  asyncTestCase.continueTesting();
<a name="line272"></a>  if (result instanceof Error) {
<a name="line273"></a>    throw result;
<a name="line274"></a>  }
<a name="line275"></a>  mockClock.tick();
<a name="line276"></a>}
<a name="line277"></a>
<a name="line278"></a>function waitForAsync(msg) {
<a name="line279"></a>  asyncTestCase.waitForAsync(msg);
<a name="line280"></a>
<a name="line281"></a>  // The mock clock must be advanced far enough that all timeouts added during
<a name="line282"></a>  // callbacks will be triggered. 1000ms is much more than enough.
<a name="line283"></a>  mockClock.tick(1000);
<a name="line284"></a>}
<a name="line285"></a>
<a name="line286"></a>function waitForEvent(type, target) {
<a name="line287"></a>  var d = new goog.async.Deferred();
<a name="line288"></a>  goog.events.listenOnce(target, type, goog.bind(d.callback, d, target));
<a name="line289"></a>  return d;
<a name="line290"></a>}
<a name="line291"></a>
<a name="line292"></a>function waitForError(type, target) {
<a name="line293"></a>  var d = new goog.async.Deferred();
<a name="line294"></a>  goog.events.listenOnce(
<a name="line295"></a>      target, goog.fs.FileSaver.EventType.ERROR, function(e) {
<a name="line296"></a>        assertEquals(type, target.getError().code);
<a name="line297"></a>        d.callback(target);
<a name="line298"></a>      });
<a name="line299"></a>  return d;
<a name="line300"></a>}
<a name="line301"></a>
<a name="line302"></a>function checkReadyState(expectedState, writer) {
<a name="line303"></a>  assertEquals(expectedState, writer.getReadyState());
<a name="line304"></a>}
<a name="line305"></a>
<a name="line306"></a>function checkPositionAndLength(expectedPosition, expectedLength, writer) {
<a name="line307"></a>  assertEquals(expectedPosition, writer.getPosition());
<a name="line308"></a>  assertEquals(expectedLength, writer.getLength());
<a name="line309"></a>}
<a name="line310"></a>
<a name="line311"></a>function checkLastModified(expectedTime) {
<a name="line312"></a>  assertEquals(expectedTime, file.lastModifiedDate.getTime());
<a name="line313"></a>}
<a name="line314"></a>
<a name="line315"></a>function writeString(str, writer) {
<a name="line316"></a>  writer.write(new goog.testing.fs.Blob(str));
<a name="line317"></a>}
<a name="line318"></a>
<a name="line319"></a>function writeAndWait(str, writer) {
<a name="line320"></a>  writeString(str, writer);
<a name="line321"></a>  return waitForEvent(goog.fs.FileSaver.EventType.WRITE_END, writer);
<a name="line322"></a>}
</pre>


</body>
</html>
